﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GiaoDien.Admin
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        ServiceKD.ServiceSoapClient proxy = new GiaoDien.ServiceKD.ServiceSoapClient("ServiceSoap");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                ServiceKD.NhaCungCap[] dsncc = proxy.layDanhSachNhaCungCap();
                drNCC.DataSource = dsncc;
                drNCC.DataTextField = "tenNhaCungCap";
                drNCC.DataValueField = "maNhaCungCap";
                drNCC.DataBind();

                drNCC2.DataSource = dsncc;
                drNCC2.DataTextField = "tenNhaCungCap";
                drNCC2.DataValueField = "maNhaCungCap";
                drNCC2.DataBind();

                ServiceKD.DanhMucSanPham[] dsdm = proxy.layDanhMuc();
                drDanhMuc.DataSource = dsdm;
                drDanhMuc.DataTextField = "tenDanhMuc";
                drDanhMuc.DataValueField = "maDanhMuc";
                drDanhMuc.DataBind();
                int MaNCC = Int32.Parse(drNCC.SelectedValue.ToString());
                LoadSanPhamTheoNCC(MaNCC);
            }
            else
            {
                int MaNCC = Int32.Parse(drNCC.SelectedValue.ToString());
                LoadSanPhamTheoNCC(MaNCC);

            }
        }
        protected void btnTimSanPham_Click(object sender, EventArgs e)
        {
            int MaNCC = Int32.Parse(drNCC.SelectedValue.ToString());

            LoadSanPhamTheoNCC(MaNCC);
        }


        public void LoadSanPhamTheoNCC(int MaNCC)
        {
            ServiceKD.SanPham[] dssp = proxy.laySanPhamTheoNhaCungCap(MaNCC);

            CollectionPager1.PageSize = 6;
            CollectionPager1.DataSource = dssp;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();
        }
        protected void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtGiaBanQuiDinh.Text = "";
            txtGiaMua.Text = "";
            txtHinhAnh.Text = "";
            txtMoTa.Text = "";
            txtNguongDuoi.Text = "0";
            txtNguongTren.Text = "0";
            txtTenSanPham.Text = "";
            txtSoLuong.Text = "0";
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            ServiceKD.SanPham temp = new GiaoDien.ServiceKD.SanPham();
            temp.tenSanPham = txtTenSanPham.Text;
            temp.soLuong = Int32.Parse(txtSoLuong.Text);
            temp.giaMua = Double.Parse(txtGiaMua.Text);
            temp.giaBanQuiDinh = Double.Parse(txtGiaBanQuiDinh.Text);
            temp.maDanhMuc = Int32.Parse(drDanhMuc.SelectedValue.ToString());
            temp.maNhaCungCap = Int32.Parse(drNCC2.SelectedValue.ToString());
            temp.moTaSanPham = txtMoTa.Text;
            temp.nguongDuoi = Int32.Parse(txtNguongDuoi.Text);
            temp.nguongTren = Int32.Parse(txtNguongTren.Text);
            temp.hinhAnh = txtHinhAnh.Text;
            temp.ngayKiKetHopDong = DateTime.Now;
            temp.thoiGianHopDong = DateTime.Now;
            temp.maTinhTrang = 1;

            bool kq = proxy.themSanPham(temp);

            if (kq)
            {

                lblThongBao.Text = "Thêm thành công";
                ServiceKD.KhachHang kh = (ServiceKD.KhachHang)Session["DangNhap"];
                ServiceKD.TableLog tl = new GiaoDien.ServiceKD.TableLog();
                tl.ngayGio = DateTime.Now;
                tl.tenBang = "SanPham";
                tl.hanhDong = "Thêm";
                tl.maNguoiDung = kh.maKhachHang;
                tl.maLog = 10;
                
                bool kqtl = proxy.themTableLog(tl);
                if(kqtl)
                {
                }
            }
            else
                lblThongBao.Text = "Thất bại";
        }
    }
}
